require.config({
    baseUrl: 'js/common',
    paths: {
        "monthData": "../data/monthData",
        "pagin": "./pagin",
        "moneyData": "../data/moneyData",
        "validate": "./validate"
    }
});
define(['monthData', 'moneyData', 'pagin', 'validate'], function(Months, Money, pagin, VD) {
    // var baseUrl = defaultStr();
    /*月份管理模块*/
    var month = new Vue({
        el: '#month-container',
        data: {
            years: [],
            months: [],
            dates: [{
                "helpMonth": 9,
                "helpYear": 2016
            }],
            selected: '',
            je: '',
            maxJe: 0
        },
        methods: {
            /*初始化列表*/
            init: function() {
                var _self = this;
                // 获取当年的年月,设置select
                var myDate = new Date();
                var year = myDate.getFullYear();
                for (var i = -5; i < 5; i++) {
                    _self.years.push(year + i);
                }
                for (var i = 1; i <= 12; i++) {
                    _self.months.push(i);
                }
                /*获取所有年月，初始化已添加年月*/
                _self._getAllDates();
                /*获取最大金额*/
                _self.getJe();
            },
            /*像服务器添加一条新的数据,并更新列表*/
            addMonth: function() {
                var _self = this;
                _self.selected.helpYear.toString();
                _self.selected.helpMonth.toString();
                Months.addMonth(_self.selected).done(
                    function(data) {
                        console.log(data);
                        alert(data.message)
                        setTimeout(_self._getAllDates, 1000);

                    })
            },
            /*删除月份*/
            delete: function(el, id) {
                var _self = this;
                id = id.toString();
                Months.delMonth(id).done(
                    function(data) {
                        alert(data.message);
                        setTimeout(_self._getAllDates, 1000);
                    }
                );
            },
            /*设置金额*/
            setMoney: function() {
                var _self = this;
                var je = {
                    "je": parseInt(_self.je)
                };
                if (VD.isNull(_self.je, "金额") && VD.legalNum(_self.je, "金额")) {
                    Money.setMoney(je).done(
                        function(data) {
                            if (data.status == 1) {
                                _self.maxJe = _self.je;
                            }
                            alert(data.message);
                            // alert("设置成功！");
                        }
                    );
                }
            },
            /*请求服务器，获取所有年月*/
            _getAllDates: function() {
                var _self = this;
                _self.dates = [];
                Months.getMonth().done(
                    function(data) {
                        $.each(data.body, function(i, value) {
                            _self.dates.push(value);
                        });
                    }
                );
            },
            /*获取金额上限*/
            getJe: function() {
                var _self = this;
                Money.getMoeny().done(function(data) {
                    console.log(data);
                    if (data.status == 1) {
                        _self.maxJe = data.body.je;

                    }
                })
            }
        }
    })
    month.init();
});